package com.xc.ezplan.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xc.ezplan.admin.entity.AdminLocalStorage;
import com.xc.ezplan.admin.service.AdminLocalStorageService;
import com.xc.ezplan.admin.vo.AdminLocalStorageVo;
import com.xc.ezplan.core.base.BaseSearchBody;
import com.xc.ezplan.core.base.EzResult;
import com.xc.ezplan.core.exception.BadRequestException;
import com.xc.ezplan.core.utils.FileUtil;
import com.xc.ezplan.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lixingchen
 * @date 2022/11/21
 * @description 本地存储管理
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "本地存储")
@RequestMapping("/localStorage")
public class AdminLocalStorageController {

    private final AdminLocalStorageService adminLocalStorageService;

    @PostMapping
    @ApiOperation("上传文件")
    @SaCheckPermission("storage.upload")
    public EzResult<Object> createFile(@RequestParam String name, @RequestParam("file") MultipartFile file){
        adminLocalStorageService.create(name, file);
        return EzResult.ok();
    }

    @Log("删除文件")
    @DeleteMapping
    @ApiOperation("多选删除")
    @SaCheckPermission("storage.delete")
    public EzResult<Object> deleteFile(@RequestBody List<Long> ids) {
        adminLocalStorageService.deleteAll(ids);
        return EzResult.ok();
    }

    @PutMapping
    @Log("修改文件")
    @ApiOperation("修改文件")
    public EzResult<Object> updateFile(@Validated @RequestBody AdminLocalStorageVo adminLocalStorageVo){
        adminLocalStorageService.updateFile(adminLocalStorageVo);
        return EzResult.ok();
    }

    @PostMapping("/page")
    @ApiOperation("查询文件")
    public EzResult<Object> pageQueryFile(@RequestBody BaseSearchBody<AdminLocalStorage> baseSearchBody){
        return EzResult.ok(adminLocalStorageService.pageQueryFile(baseSearchBody));
    }

    @ApiOperation("上传图片")
    @PostMapping("/pictures")
    public EzResult<Object> uploadPicture(@RequestParam MultipartFile file){
        // 判断文件是否为图片
        String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
        if(!FileUtil.IMAGE.equals(FileUtil.getFileType(suffix))){
            throw new BadRequestException("只能上传图片");
        }
        adminLocalStorageService.create(null, file);
        return EzResult.ok();
    }

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportFile(HttpServletResponse response, AdminLocalStorageVo adminLocalStorageVo) throws IOException {
        adminLocalStorageService.download(adminLocalStorageVo, response);
    }
}